package com.atguigu.lease.web.admin.custom.converter;

import com.atguigu.lease.model.enums.ItemType;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.util.Arrays;

//@Component
public class ItemTypeConverter implements Converter<String, ItemType> {
    @Override
    public ItemType convert(String source) {
        // 枚举有一个静态方法：values() ， 返回这个枚举类型中所有定义好的枚举
        //ItemType.values()

        return Arrays.stream(ItemType.values())
                .filter(itemType -> {return source.equals(""+itemType.getCode());})
                .findFirst()
                .orElseThrow(IllegalArgumentException::new);

    }
}
